package cn.itsource.org.controller;

import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.FastDfsApiOpr;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileUploadManager {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        System.out.println(file);
        //获取上传文件的后缀名
        String wholeName = file.getOriginalFilename();//这个获取的就是上传时的文件的全名（包括后缀）
        String extName = StringUtils.substringAfterLast(wholeName, ".");
        //通过调用Fastdfs工具类，进行文件的上传
        String upload = FastDfsApiOpr.upload(file.getBytes(), extName);
//        String prefix_url = "http://47.95.117.210:8888";
        return AjaxResult.me().setData(upload);
//        return AjaxResult.me().setData(prefix_url + upload);
    }
}
